﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Utility
{
    public static class MyEmail
    {
        //提供邮件服务功能的服务器地址
        private static string smtpServer = "Smtp.qq.com";
        private static string mailFrom = "359282724@qq.com";
        //邮件的用户名
        private static string mailUserName = "359282724@qq.com";
        //邮件的密码
        private static string mailUserPass = "523285235";
           
        #region
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailTo">收件人:359282724@qq.com</param>
        /// <param name="mailSubject">邮件主题</param>
        /// <param name="mailBody">邮件正文</param>
        /// <param name="isHtmlMail">是否发送html邮件</param>
        /// <returns></returns>
        public static bool SendEmail(string mailTo, string mailSubject, string mailBody, bool isHtmlMail)
        {
            try
            {
                System.Net.Mail.SmtpClient client = new SmtpClient();

                client.Host = smtpServer;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(mailUserName, mailUserPass);
                //星号改成自己邮箱的密码 
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                System.Net.Mail.MailMessage message = new MailMessage(mailFrom, mailTo);
                message.Subject = mailSubject;
                message.Body = mailBody;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = isHtmlMail;

                //添加附件 
                //Attachment data = new Attachment("附件地址如：e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet); 
                //message.Attachments.Add(data); 
                //JS.Alert("mailFrom:" + mailFrom + "<br />" + "MailTo:" + mailTo + "<br />" + "smtpServer:" + smtpServer + "<br />" + "UserName" + mailUserName);
                
                //开始发送
                client.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        #region
        /// <summary>
        /// 群发邮件
        /// </summary>
        /// <param name="mailTo">收件人</param>
        /// <param name="mailSubject">邮件主题</param>
        /// <param name="mailBody">邮件正文</param>
        /// <param name="isHtmlMail">是否发送html邮件</param>
        /// <param name="mp">邮件的优先级</param>
        /// <returns></returns>
        public static bool SendEmails(string[] mailTo, string mailSubject, string mailBody, bool isHtmlMail)
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                //收件人
                foreach (string Mail in mailTo)
                    mailMessage.To.Add(new MailAddress(Mail));
                //主题
                mailMessage.Subject = mailSubject;
                //正文
                mailMessage.Body = mailBody;
                //是否发送html正文
                mailMessage.IsBodyHtml = isHtmlMail;
                //发件人
                //mailFrom = Config.MailFrom;
                mailMessage.From = new MailAddress(mailFrom);

                mailMessage.Priority = MailPriority.High;


                //SmtpClient是发送邮件的主体，这个构造函数是告知SmtpClient发送邮件时使用哪个SMTP服务器
                SmtpClient mailClient = new SmtpClient(smtpServer);
                //将认证实例赋予mailClient,也就是访问SMTP服务器的用户名和密码
                mailClient.Credentials = new NetworkCredential(mailUserName, mailUserPass);
                //最终的发送方法
                mailClient.Send(mailMessage);
                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion
    }
}
